#include <QtGui/QApplication>
#include "mainwindow.h"

#include <QTime>
#include <stdio.h>

//gestion du niveau de debug
typedef enum VerboseLevel_e {
    VERBOSE_SILENT,     ///< 0- No outputs at all
    VERBOSE_FATAL,      ///< 1- Only fatal outputs (default)
    VERBOSE_CRITICAL,   ///< 2- Critical and fatal outputs
    VERBOSE_WARN,       ///< 3- Warning and above outputs are printed
    VERBOSE_FULL        ///< 4- Every output are printed
} VerboseLevel_e;

typedef struct TraceConfig_T {
    TraceConfig_T() { e_verbosity = VERBOSE_FATAL; }
    VerboseLevel_e e_verbosity;
} TraceConfig_T;


/*
 *** Global  Var ***
 */
static TraceConfig_T Str_traceConfig;

void myQtMessageFilter(QtMsgType type, const char *msg)
{
    QTime now = QTime::currentTime ();
    QString nowStr = now.toString();

    switch (type) {
    case QtDebugMsg:
        if( Str_traceConfig.e_verbosity >= VERBOSE_FULL )
            fprintf(stderr, "[D](%s) %s\n",qPrintable(nowStr), msg);
        break;
    case QtWarningMsg:
        if( Str_traceConfig.e_verbosity >= VERBOSE_WARN )
            fprintf(stderr, "[W](%s) %s\n",qPrintable(nowStr), msg);
        break;
    case QtCriticalMsg:
        if( Str_traceConfig.e_verbosity >= VERBOSE_CRITICAL )
            fprintf(stderr, "[C](%s) %s\n",qPrintable(nowStr), msg);
        break;
    case QtFatalMsg:
        if( Str_traceConfig.e_verbosity >= VERBOSE_FATAL )
            fprintf(stderr, "[F](%s) %s\n",qPrintable(nowStr), msg);
        abort();
        break;

    default:
        if( Str_traceConfig.e_verbosity >= VERBOSE_FULL )
            fprintf(stderr, "[?](%s) %s\n", qPrintable(nowStr), msg);
        break;
    }
}


/*
 * Entry Point
 *
 */
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;
    QStringList arguments;

    arguments = a.arguments();
    qInstallMsgHandler(myQtMessageFilter);

    a.addLibraryPath("./plugins/"); //to find plugins in local folder

    //par defaut full debug, retirer cette ligne en prod
    Str_traceConfig.e_verbosity = (VerboseLevel_e)(VERBOSE_FULL+1);

    if( arguments.contains("--verbosity") )
    {
        Str_traceConfig.e_verbosity = qBound(
                VERBOSE_SILENT, // 0
                (VerboseLevel_e) arguments.at( arguments.indexOf( "--verbosity" ) + 1 ).toInt(),
                VERBOSE_FULL    // 4
                );
    }


    w.show();

    return a.exec();
}
